package at.jsug.topicvoting.gwt1.client.view;

import at.jsug.topicvoting.gwt1.client.vo.Topic;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.kaboomerang.gwt.rest.client.handler.ResourceResponseHandler;
import com.kaboomerang.gwt.rest.client.resource.Resource;
import com.kaboomerang.gwt.rest.client.resource.ResourceCollection;
import com.kaboomerang.gwt.rest.client.resource.ResourceFactory;
import com.kaboomerang.gwt.rest.client.resource.impl.RailsJsonResourceCollection;

public class DelmeRest {
	
	public DelmeRest(final HorizontalPanel controlBar) {
		
		final Button btn = new Button("Fetch single topic");
		btn.addClickListener(new ClickListener() { public void onClick(Widget sender) {
// 			onFetchSingle();
			onAddSingle();
		}});
		
		controlBar.add(btn);
	}

	private static final String BASE_URL = "http://10.0.1.4/~phudy/PhpDev/JsugVotingRestBackend/?";
	
	private void onAddSingle() {
		final String url = BASE_URL + "/topics"; 
		final ResourceFactory factory = new ResourceFactory() {
			public Resource create() {
				return (Resource) GWT.create(TopicsResource.class);
			}
		};
		
		final ResourceCollection topic = new RailsJsonResourceCollection(url, factory);
		final Topic t = Topic.newNeedTopic(1, "fromAddSingle", 13, false);
		topic.create(new TopicsResource(t), new ResourceResponseHandler() {
			public void onSuccess(Resource resource) {
				// TODO what to do?
			}
			public void onError(Request request, Throwable exception) {
				// TODO onError
			}
		});
	}
	
	private void onFetchSingle() {
		System.out.println("DelmeRest.onRequestClicked()");
		ResourceCollection topic = new RailsJsonResourceCollection(
				BASE_URL + "/topics/", new ResourceFactory() {
					public Resource create() {
						return (Resource) GWT.create(TopicsResource.class);
					}
				});

		topic.find(1, new ResourceResponseHandler() {
			public void onSuccess(Resource resource) {
				final TopicsResource topicsResource = (TopicsResource) resource;
				final Topic topic = topicsResource.asTopic();
				
				Window.alert("Topics resource #"+topic.getId()+": " + topic.getTitle());
			}

			public void onError(Request request, Throwable exception) {
				Window.alert("Something went wrong.");
				System.out.println(request);
				System.out.println();
				exception.printStackTrace();
			}
		});

	}
	
	private static class TopicsResource implements Resource {

		private Topic topic;

		public TopicsResource() {
			
		}
		public TopicsResource(Topic topic) {
			this.topic = topic;
		}
		
		public int getId() {
			// TODO Auto-generated method stub
			return 0;
		}
		
		private String x;
		
		public Topic asTopic() {
			JSONValue json = JSONParser.parse(this.x);
			// JSONValue json = JSONParser.parse(" { \"id\" : \"1\", \"title\": \"foobarYEAHfoobar\" }");
			
			JSONObject obj = (JSONObject) json;
			
			JSONString val = (JSONString) obj.get("id");
			final long id = Long.parseLong(val.stringValue());
			
			final String title = ((JSONString) obj.get("title")).stringValue();
			
			
			final int votes = 0;
			final boolean yetVoted = false;
			
			return Topic.newNeedTopic(id, title, votes, yetVoted);
		}

		public void populateFromRepresentation(String representation) {
			System.out.println("populateFromRepresentation: representation=["+representation+"]");
			x = representation;
		}

		public String toRepresentation() {
			// return "<toRepresentation ["+x+"]>";
			return "{ \"id\" : \"" + this.topic.getId() + "\", \"title\": \"" + this.topic.getTitle() + "\" }";
		}
		
	}
}
